/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;

import java.io.*;
import java.lang.Thread;

/**
 *
 * @author Sinem
 */
public class WebSpider {

    private String m_sURL;

    public WebSpider (String sURL)
    {
        m_sURL = new String(sURL);
    }

    public synchronized boolean downloadWholeSite()
    {
        boolean bRetVal = false;
        String line = null;

        /*
         * --level = 0 --> recursive download , infinitely
         */
        //--page-requisites
        //use -w option
        StringBuffer commandString = new StringBuffer("wget --recursive --level=5 ");
        commandString.append(m_sURL);        
        try {
            Process process = Runtime.getRuntime().exec(commandString.toString());

            InputStream stderr = process.getErrorStream();
            InputStreamReader isr = new InputStreamReader(stderr);
            BufferedReader br = new BufferedReader(isr);                     
            while ( (line = br.readLine()) != null)
                System.out.println(line);            
            int exitVal = process.waitFor();
            System.out.println("Download Process Exit Value: " + exitVal);
            if (exitVal == 0)
                bRetVal = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        catch(InterruptedException e){e.printStackTrace();}

        return bRetVal;
    }
}
